package com.edgar.designpattern.proxy.order;

public class OrderProxy implements Order {

    private int orderId;
    
    public OrderProxy(int orderId) {
        super();
        this.orderId = orderId;
    }

    @Override
    public String getCustomerId() {
        OrderData od = DB.getOrderData(orderId);
        return od.getCustomerId();
    }

    @Override
    public void addItem(Product p, int quantity) throws Exception {
        ItemData id = new ItemData(orderId, quantity, p.getSku());
        DB.store(id);
    }

    @Override
    public int total() throws Exception {
        OrderImpl imp = new OrderImpl(getCustomerId());
        ItemData[] itemDataArray = DB.getItemForOrder(orderId);
        for (int i = 0; i < itemDataArray.length; i++) {
            ItemData item = itemDataArray[i];
            imp.addItem(new ProductProxy(item.getSku()), item.getQuantity());
        }
        return imp.total();
    }

    public int getOrderId() {
        return orderId;
    }

}
